package com.wxy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxy.entity.Saledata;
import com.wxy.entity.Saledetaildata;
import com.wxy.entity.Saleweekdata;
import com.wxy.mapper.*;
import com.wxy.selectUser.SelectUser;
import com.wxy.service.SaledetaildataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wxy
 * @since 2022-01-12
 */
@RestController
@RequestMapping("/saleDetailData")
public class SaledetaildataController {

    @Autowired
    SaledetaildataMapper saledetaildataMapper;

    @Autowired
    SaleweekdataMapper saleweekdataMapper;

    @Autowired
    SaledetaildataService saledetaildataService;
    //细节数据
    @GetMapping("/getDetailData/{page}")
    public IPage getDetailData(
            HttpServletRequest request,
            @PathVariable("page") Integer pageNum
    ){
        //取出page
        QueryWrapper qw =  SelectUser.saledetailDataSelectUser(request.getHeader("userId"));
        qw.orderByDesc("saleDetailDataId");
        IPage page = new Page(pageNum,10);
        return saledetaildataMapper.selectPage(page,qw);
    }


    //细节数据按时间查询
    @GetMapping("/getDetailDataByTime/{time}")
    public List getDetailDataByTime(
            HttpServletRequest request,
            @PathVariable("time") Date time
    ){
        //取出page
        QueryWrapper qw =  SelectUser.saledetailDataSelectUser(request.getHeader("userId"));
        qw.eq("detailTime",time);
        return saledetaildataMapper.selectList(qw);
    }

    //细节数据按负责人姓名查找
    @GetMapping("/getDetailDayByHeader/{name}")
    public List getDetailDayByHeader(
            HttpServletRequest request,
            @PathVariable("name") String name
    ){
        //取出page
        QueryWrapper qw =  SelectUser.saledetailDataSelectUser(request.getHeader("userId"));
        qw.eq("detailProjectHeader",name);
        return saledetaildataMapper.selectList(qw);
    }

    //细节数据更新
    @PostMapping("/updateDetailData")
    public Boolean updateDetailData(
            @RequestBody Saledetaildata saledetaildata
    ){
        saledetaildataMapper.updateById(saledetaildata);
        return true;
    }

    //细节数据添加
    @PostMapping("/insertDetailData")
    public  Boolean insertDetailData(
            @RequestBody Saledetaildata saledetaildata,
            HttpServletRequest request
    ){
        saledetaildata.setUserId(Integer.valueOf(request.getHeader("userId").toString()));
        saledetaildata.setDetailProjectProfit(saledetaildata.getDetailProjectPice()-saledetaildata.getDetailProjectCost());
        saledetaildata.setSaleDetailDataId(null);
        saledetaildataMapper.insert(saledetaildata);
        return true;
    }

}

